//获取应用实例
const app = getApp();
var config = app.globalData.config;

// pages/join/join.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowTips: true,
    pageIndex: 1,
    pageSize: 50,
    activityId: 0,
    joinUser: [],
    count: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var {
      activityId
    } = options;
    if (!!activityId) {
      this.setData({
        activityId: activityId
      })
      this.getActivityUser()
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.isShowTips) {
      return;
    }
    var page = this.data.pageIndex + 1;
    this.setData({
      pageIndex: page
    })
    this.getActivityUser();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getActivityUser() {
    var that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: config.activityUserUrl,
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        id: that.data.activityId,
        pageIndex: that.data.pageIndex,
        pageSize: that.data.pageSize
      },
      success: function(res) {
        // 回调函数
        var moment_list = that.data.joinUser;
        for (var i = 0; i < res.data.data.users.length; i++) {
          moment_list.push(res.data.data.users[i]);
        }
        // 设置数据
        that.setData({
          joinUser: moment_list,
          count: res.data.data.count,
          isShowTips: res.data.data.users.length == 0 || that.data.pageSize > res.data.data.count
        })
      },
      fail(res) {},
      complete(res) {
        wx.hideLoading();
      }
    })
  }
})